//
//  SCB_Today_View.m
//  Time
//
//  Created by dllo on 16/7/13.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "SCB_Today_View.h"
#import "Today_todayContentCollectionViewCell.h"
#import "Today_debrisContentCollectionViewCell.h"

@interface SCB_Today_View ()
<
UICollectionViewDataSource,
UICollectionViewDelegate,
Today_todayContentCollectionViewCellDelegate,
Today_debrisContentCollcetionViewCellDelegate
>



@end

@implementation SCB_Today_View
-(void)dealloc{
    [_contentCollectionView release];
    [_modelArray release];
    _contentCollectionView.dataSource = nil;
    _contentCollectionView.delegate = nil;
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.modelArray = [NSMutableArray arrayWithCapacity:5];
        self.debrisModelArray = [NSMutableArray arrayWithCapacity:5];
        
        [self createCollection];
        
    }
    return self;
}

-(void)createCollection{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(SWIDTH, SHEIGHT - 88);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    /**
     三个页面的翻转的collectionView;
     
     - returns: -_-||
     */
    self.contentCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, SHEIGHT - 88) collectionViewLayout:flowLayout];
    self.contentCollectionView.delegate = self;
    self.contentCollectionView.dataSource = self;
    self.contentCollectionView.pagingEnabled = YES;
    self.contentCollectionView.backgroundColor = [UIColor whiteColor];
    self.contentCollectionView.contentOffset = CGPointMake(SWIDTH, 0);
    [self addSubview:self.contentCollectionView];
    
    /**
     *  内容CollectionView中的三个页面注册基础的重用池.
     */
    [self.contentCollectionView registerClass:[Today_todayContentCollectionViewCell class] forCellWithReuseIdentifier:@"today_today_content_cell"];
    [self.contentCollectionView registerClass:[Today_debrisContentCollectionViewCell class] forCellWithReuseIdentifier:@"today_debris_content_cell"];
    [self.contentCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"today_dynamic_content_cell"];
    
    [flowLayout release];
    [_contentCollectionView release];
    
}
// 协议方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 2;
}
// cell  有大量需要修改的东西

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            NSLog(@"第一个页面");
            Today_debrisContentCollectionViewCell *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"today_debris_content_cell" forIndexPath:indexPath];
            cell1.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0  blue:arc4random() % 256 / 255.0 alpha:1.0];
            cell1.debrisModelArray = self.debrisModelArray;
            cell1.delegate = self;
            return cell1;
            break;
        case 1:
            NSLog(@"第二个页面");
            Today_todayContentCollectionViewCell *cell2 = [collectionView dequeueReusableCellWithReuseIdentifier:@"today_today_content_cell" forIndexPath:indexPath];
            cell2.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0  blue:arc4random() % 256 / 255.0 alpha:1.0];
            cell2.modelArray = self.modelArray;
            cell2.delegate = self;
            return cell2;

            break;
//        case 2:
//            NSLog(@"第三个页面");
//            UICollectionViewCell *cell3 = [collectionView dequeueReusableCellWithReuseIdentifier:@"today_dynamic_content_cell" forIndexPath:indexPath];
//            cell3.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0  blue:arc4random() % 256 / 255.0 alpha:1.0];
//            return cell3;
//            break;
        default:
            NSLog(@"???");
            UICollectionViewCell *cell4 = [collectionView dequeueReusableCellWithReuseIdentifier:@"today_dynamic_content_cell" forIndexPath:indexPath];
            cell4.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0  blue:arc4random() % 256 / 255.0 alpha:1.0];
            return cell4;
            break;
    }
}

-(void)setModelArray:(NSMutableArray *)modelArray{
    if (_modelArray != modelArray) {
        [_modelArray release];
        _modelArray = [modelArray retain];
    }
    
    [self.contentCollectionView reloadData];
}

-(void)setDebrisModelArray:(NSMutableArray *)debrisModelArray{
    if (_debrisModelArray != debrisModelArray){
        [_debrisModelArray release];
        _debrisModelArray = [debrisModelArray retain];
    }
    [self.contentCollectionView reloadData];
}

-(void)loadNewData:(BOOL)isDown{
    [self.delegate loadNewData:isDown];
}

-(void)loadNewDataForDebris:(BOOL)isDown{
    [self.delegate loadNewDataForDebris:isDown];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSInteger offset = scrollView.contentOffset.x / SWIDTH;
    if (offset == 0) {
        self.offsetBlock(YES);
    } else if (offset == 1){
        self.offsetBlock(NO);
    }
}

-(void)pushToDetail:(NSString *)contentId{
    [self.delegate pushToDetail:contentId];
}

-(void)pushToMusic:(NSString *)contentId{
    [self.delegate pushToMusic:contentId];
}

@end
